package ru.p03.tvipuskeditor;

import java.util.Map;

import com.vaadin.annotations.Theme;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
@Theme("tvipuskeditor")
public class TvipuskeditorUI extends UI {
	
	TvipuskEditor te;
	
	@Override
	protected void init(VaadinRequest request) {
		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		setContent(layout);

		Button button = new Button("Test save");
		button.addClickListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				//layout.addComponent(new Label("Thank you for clicking"));
				te.sendGetTextRequest();
			}
		});
		layout.addComponent(button);
		
		
		Button buttonEnabledTrue = new Button("enabledTrue");
		buttonEnabledTrue.addClickListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				//layout.addComponent(new Label("Thank you for clicking"));
				te.sendEnabled(true);
			}
		});
		layout.addComponent(buttonEnabledTrue);

		Button buttonEnabledFalse = new Button("enabledFalse");
		buttonEnabledFalse.addClickListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				//layout.addComponent(new Label("Thank you for clicking"));
				te.sendEnabled(false);
			}
		});
		layout.addComponent(buttonEnabledFalse);
	
		Button buttonFilename = new Button("buttonFilename");
		buttonFilename.addClickListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				//layout.addComponent(new Label("Thank you for clicking"));
				te.sendFilename("filename dssdds");
			}
		});
		layout.addComponent(buttonFilename);
			
		Button buttonSetWidth = new Button("setWidth");
		buttonSetWidth.addClickListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				te.setTeWidth("700px");   
				//te.markAsDirty();			
			}
		});
		layout.addComponent(buttonSetWidth);

		Button buttonSetText = new Button("buttonSetText2");
		buttonSetText.addClickListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				te.setXmlText("this is xml text ");
			}
		});
		layout.addComponent(buttonSetText);
		
		te = new TvipuskEditor();
		te.setTeWidth("1200px");
		te.setTeHeight("500px");

		layout.addComponent(te);

		te.addValueChangeListener(new ValueChangeListener(){
			@Override
			public void valueChange(ValueChangeEvent event){
				System.out.println("value change: " + event.getProperty().getValue());

				Map<String,String> valMap = (Map<String, String>) event.getProperty().getValue();
				/*
				if (valMap.containsKey(TvipuskEditor.REQUEST_FILENAME)){
					//tv.setFileAttrs("filename", "duration");
					String fileName = valMap.get(TvipuskEditor.REQUEST_FILENAME); 
					System.out.println("Было запрошено имя файла: ");
					//onFileRequest();
				}

				if (valMap.containsKey(TvipuskEditor.RETURN_FILENAME)){
					//tv.setFileAttrs("filename", "duration");

					String fName = (String)valMap.get(VTvipuskEditor.RETURN_FILENAME);
					int fNameInd = fName.lastIndexOf(':');
					fName = fName.substring(0,fNameInd);

					System.out.println("Получили имя файла: " + fName);
				}
*/
				if (valMap.containsKey("returntext")){
					String returntext = valMap.get("returntext");
					System.out.println("returntext: " + returntext);

					/*
					if("Approve".equals(saveAction)){
						//sdsdsdsd
					}
					if("Save".equals(saveAction)){
						//sdsdsdsd
					}*/
				}
			}
		});		
		
		
	}
}